//
//  ViewControllerForVideoPlay.m
//  世界
//
//  Created by admin on 16/2/14.
//  Copyright © 2016年 admin. All rights reserved.
//

#import "ViewControllerForVideoPlay.h"
#import "CellForVideoRecommendation.h"
#import <UIImageView+WebCache.h>
#import "NetworkingHandler.h"

#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height

@interface ViewControllerForVideoPlay ()<UITableViewDataSource, UITableViewDelegate>


@property (nonatomic, retain)UIView *viewForVideo;
@property (nonatomic, retain) AVPlayer *player;
@property (nonatomic, retain) AVPlayerLayer *playerLayer;
@property (nonatomic, retain) AVPlayerItem *playerItem;
@property (nonatomic, retain) UISlider *sliderForVideoProgress;
@property (nonatomic, retain) UIButton *playButton;
@property (nonatomic, retain) UIButton *buttonForScale;
@property (nonatomic, retain) UIButton *buttonForBack;
@property (nonatomic, retain) UILabel *lableForTime;

@property (nonatomic, assign) BOOL isPlay;
@property (nonatomic, assign) BOOL isTap;
@property (nonatomic, assign) BOOL isScale;
@property (nonatomic, assign) NSInteger number;

@property (nonatomic, retain) UIButton *buttonForState;
@property (nonatomic, retain) UIView *viewForControlButton;
@property (nonatomic, retain) UIProgressView *progressViewForVideoCache;
@property (nonatomic, retain) NSString *strForTotalTime;
@property (nonatomic, retain) NSDateFormatter *dateFormatter;

@property (nonatomic ,strong) id playbackTimeObserver;



@property (nonatomic, retain) NSMutableArray *arrTitle;
@property (nonatomic, retain) NSMutableArray *arrMp4_url;
@property (nonatomic, retain) NSMutableArray *arrLength;
@property (nonatomic, retain) NSMutableArray *arrCover;

@property (nonatomic, retain) UITableView *tabView;



@end

@implementation ViewControllerForVideoPlay


#pragma mark 重写dealloc方法
- (void)dealloc
{
    [_mp4_url release];
    
    [_viewForVideo release];
    
    [_player release];
    
    [_playerLayer release];
    
    [_playerItem release];
    
    [_sliderForVideoProgress release];
    
    [_playButton release];
    
    [_lableForTime release];
    
    [_lableForTitle release];
    
    [_arrTitle release];
    
    [_arrMp4_url release];
    
    [_arrLength release];
    
    [_arrCover release];
    
    [_tabView release];
    
    [self removeObservers];
    
    [super dealloc];
}




#pragma mark 视图的声明周期 >>>>>>
#pragma mark 视图加载完成
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.arrTitle = [NSMutableArray array];
    
    self.arrMp4_url = [NSMutableArray array];
    
    self.arrLength = [NSMutableArray array];
    
    self.arrCover = [NSMutableArray array];
    
    self.number = kCGGlyphMax;
    
    [self handleData];
    
    [self creataControls];
    
    [self createTabView];
    
    [self createAVPlayer];
    
    [self addObservers];
    
    [self addGestureRecognizer];
}


#pragma mark 接受到内存警告!!!
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}




#pragma mark 自定义方法 >>>>>>
#pragma mark 添加装载AVPlayer的View
- (void)creataControls
{
    self.viewForVideo = [[UIView alloc] init];
    
    self.viewForVideo.frame = CGRectMake(0, 0, SCREEN_WIDTH, 300);
    
    [self.view addSubview:self.viewForVideo];
    
    [_viewForVideo release];
    
     UIColor *bgColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"视频背景竖.jpg"]];
    
    self.viewForVideo.backgroundColor = bgColor;
    
    
    self.viewForControlButton = [[UIView alloc] init];
    
    self.viewForControlButton.frame = CGRectMake(0, 260, SCREEN_WIDTH, 40);
    
    UIColor *bgColor1 = [UIColor colorWithPatternImage:[UIImage imageNamed:@"操作按钮.jpg"]];
    
    self.viewForControlButton.backgroundColor = bgColor1;
    
    [self.viewForVideo addSubview:self.viewForControlButton];
    
    [_viewForControlButton release];
    
    
    
    
    self.buttonForState = [UIButton buttonWithType:UIButtonTypeSystem];
    
    self.buttonForState.frame = CGRectMake(5, 5, 40, 30);
    
    [self.buttonForState setTitle:@"Play" forState:UIControlStateNormal];
    
    self.buttonForState.enabled = NO;
    
    [self.viewForControlButton addSubview:self.buttonForState];

    [self.buttonForState addTarget:self action:@selector(buttonForStateAction:) forControlEvents:UIControlEventTouchUpInside];
    
    [_buttonForState release];
    
    
    
    
    self.progressViewForVideoCache = [[UIProgressView alloc] init];
    
    self.progressViewForVideoCache.frame = CGRectMake(50, 19, SCREEN_WIDTH-200, 30);
    
    [self.viewForControlButton addSubview:self.progressViewForVideoCache];
    
    [_progressViewForVideoCache release];
    
    
    
    
    self.sliderForVideoProgress = [[UISlider alloc] init];
    
    self.sliderForVideoProgress.frame = CGRectMake(50, 5, SCREEN_WIDTH-200, 30);
    
    [self.sliderForVideoProgress addTarget:self action:@selector(ProgressAction:) forControlEvents:UIControlEventValueChanged];
    
    [self.viewForControlButton addSubview:self.sliderForVideoProgress];
    
    [_sliderForVideoProgress release];
    
    
    
    
    self.lableForTime = [[UILabel alloc] init];
    
    self.lableForTime.frame = CGRectMake(50 + SCREEN_WIDTH - 200 + 5, 5, SCREEN_WIDTH - (50 + SCREEN_WIDTH-150 + 5 + 5), 30);
    
    self.lableForTime.text = @"Time";
    
    self.lableForTime.font = [UIFont systemFontOfSize:14];
    
    self.lableForTime.textAlignment = NSTextAlignmentCenter;
    
    [self.viewForControlButton addSubview:self.lableForTime];
    
    [_lableForTime release];
    
    
    
    
    
    self.buttonForScale = [UIButton buttonWithType:UIButtonTypeSystem];
    
    self.buttonForScale.frame = CGRectMake(10 + 50 + SCREEN_WIDTH - 200 + 5 + SCREEN_WIDTH - (50 + SCREEN_WIDTH-150 + 5 + 5), 5, 30, 30);
    
    [self.buttonForScale setImage:[UIImage imageNamed:@"放大"] forState:UIControlStateNormal];
    
    [self.buttonForScale addTarget:self action:@selector(buttonForScaleAction:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.viewForControlButton addSubview:self.buttonForScale];
    
    
    
    
    self.buttonForBack = [UIButton buttonWithType:UIButtonTypeSystem];
    
    self.buttonForBack.frame = CGRectMake(15, 30, 40, 40);
    
    [self.viewForVideo addSubview:self.buttonForBack];
    
    [self.buttonForBack setImage:[UIImage imageNamed:@"后退"] forState:UIControlStateNormal];
    
    [self.buttonForBack addTarget:self action:@selector(buttonForBackAction:) forControlEvents:UIControlEventTouchUpInside];
    
    
    self.lableForTitle = [[UILabel alloc] init];
    
    self.lableForTitle.frame = CGRectMake(60, 30, SCREEN_WIDTH - 120, 40);
    
    self.lableForTitle.textAlignment = NSTextAlignmentCenter;
    
    self.lableForTitle.textColor = [UIColor whiteColor];
    
    [self.viewForVideo addSubview:self.lableForTitle];
    
    [_lableForTitle release];
    
}



#pragma mark 点击后退Button所触发的方法
- (void)buttonForBackAction:(UIButton *)button
{
    
    if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)])
    {
        [[UIDevice currentDevice] performSelector:@selector(setOrientation:) withObject:(id)UIInterfaceOrientationPortrait];
    }
    
    [self.player pause];
    
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}





#pragma mark 点击播放Button所触发的方法
- (void)buttonForStateAction:(UIButton *)button
{
    if (!self.isPlay)
    {
        [self.player play];
        
        [self.buttonForState setTitle:@"Stop" forState:UIControlStateNormal];
    } else
    {
        [self.player pause];
        
        [self.buttonForState setTitle:@"Play" forState:UIControlStateNormal];
    }
    self.isPlay = !self.isPlay;
}




#pragma mark 点击全屏Button所触发的方法
- (void)buttonForScaleAction:(UIButton *)button
{
     self.isScale = !self.isScale;
    if (self.isScale)
    {
        if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)])
        {
            [[UIDevice currentDevice] performSelector:@selector(setOrientation:) withObject:(id)UIInterfaceOrientationLandscapeRight];
        }
        
        [self.buttonForScale setImage:[UIImage imageNamed:@"缩小"] forState:UIControlStateNormal];
        
        self.viewForVideo.frame = [UIScreen mainScreen].bounds;
        
        self.viewForControlButton.frame = CGRectMake(0, 374, SCREEN_WIDTH, 40);
        
        self.progressViewForVideoCache.frame = CGRectMake(50, 19, SCREEN_WIDTH-200, 30);
        
        self.sliderForVideoProgress.frame = CGRectMake(50, 5, SCREEN_WIDTH-200, 30);
        
        self.playerLayer.frame = CGRectMake(0, 0, SCREEN_WIDTH, self.viewForVideo.bounds.size.height);
        
        self.lableForTime.frame = CGRectMake(50 + SCREEN_WIDTH - 200 + 5, 5, SCREEN_WIDTH - (50 + SCREEN_WIDTH-150 + 5 + 5), 30);
        
        self.buttonForScale.frame = CGRectMake(10 + 50 + SCREEN_WIDTH - 200 + 5 + SCREEN_WIDTH - (50 + SCREEN_WIDTH-150 + 5 + 5), 5, 30, 30);

        self.lableForTitle.frame = CGRectMake(60, 30, SCREEN_WIDTH - 120, 40);
        
        self.tabView.frame = CGRectMake(0, 414, SCREEN_WIDTH, SCREEN_HEIGHT - 300);
        
        UIColor *bgColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"视频背景横.jpg"]];
        
        self.viewForVideo.backgroundColor = bgColor;
        
    }
    else
    {
        if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)])
        {
            [[UIDevice currentDevice] performSelector:@selector(setOrientation:) withObject:(id)UIInterfaceOrientationPortrait];
        }
        
         [self.buttonForScale setImage:[UIImage imageNamed:@"放大"] forState:UIControlStateNormal];
        
        self.viewForVideo.frame = CGRectMake(0, 0, SCREEN_WIDTH, 300);
        
        self.viewForControlButton.frame = CGRectMake(0, 260, SCREEN_WIDTH, 40);
        
        self.progressViewForVideoCache.frame = CGRectMake(50, 19, SCREEN_WIDTH-200, 30);
        
        self.sliderForVideoProgress.frame = CGRectMake(50, 5, SCREEN_WIDTH-200, 30);
        
        self.playerLayer.frame = CGRectMake(0, 0, SCREEN_WIDTH, self.viewForVideo.bounds.size.height);
        
        self.lableForTime.frame = CGRectMake(50 + SCREEN_WIDTH - 200 + 5, 5, SCREEN_WIDTH - (50 + SCREEN_WIDTH-150 + 5 + 5), 30);
        
        self.buttonForScale.frame = CGRectMake(10 + 50 + SCREEN_WIDTH - 200 + 5 + SCREEN_WIDTH - (50 + SCREEN_WIDTH-150 + 5 + 5), 5, 30, 30);
        
        self.lableForTitle.frame = CGRectMake(60, 30, SCREEN_WIDTH - 120, 40);
        
        self.tabView.frame = CGRectMake(0, 300, SCREEN_WIDTH, SCREEN_HEIGHT - 300);
        
        UIColor *bgColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"视频背景竖.jpg"]];
        
        self.viewForVideo.backgroundColor = bgColor;
    }
}




#pragma mark 拖动进度条所触发的方法
- (void)ProgressAction:(UISlider *)sender
{
    UISlider *slider = (UISlider *)sender;
    
    CMTime changedTime = CMTimeMakeWithSeconds(slider.value, 1);
    
    [self.player seekToTime:changedTime completionHandler:^(BOOL finished) {
        
        if (self.isPlay)
        {
             [self.player play];
        }
        else
        {
            [self.player pause];
        }
    }];
}



#pragma mark 播放结束所触发的方法
- (void)moviePlayDidEnd:(NSNotification *)notification
{
    [self.player seekToTime:kCMTimeZero completionHandler:^(BOOL finished) {
        [self.sliderForVideoProgress setValue:0.0 animated:YES];
        
        //button按钮显示重新开始播放
        [self.buttonForState setTitle:@"Play" forState:UIControlStateNormal];
        
        //使点击BUtton重新开始播放
        self.isPlay = NO;
    }];
}



#pragma mark 处理数据
- (void)handleData
{
    //设置访问地址
    NSString *url = [NSString stringWithFormat:@"http://c.m.163.com/nc/video/detail/%@.html", self.vid];
    
    
    [NetworkingHandler netWorkingWithURL:url completeHandle:^(NSURLResponse *response, NSData *data ,id result) {
        
    
        
        NSArray *arr = [result objectForKey:@"recommend"];
        
        
        for (NSDictionary *dic in arr)
        {
            NSString *str1 = [dic objectForKey:@"title"];
            
            NSString *str2 = [dic objectForKey:@"mp4_url"];
            
            NSString *str3 = [dic objectForKey:@"length"];
            
            NSString *str4 = [dic objectForKey:@"cover"];
            
            
            [self.arrTitle addObject:str1];
            
            [self.arrMp4_url addObject:str2];
            
            [self.arrLength addObject:str3];
            
            [self.arrCover addObject:str4];
        }
        
            [self.tabView reloadData];
    
    }];
}





#pragma mark 创建AVPlayer
- (void)createAVPlayer
{
    NSString *urlStr=[NSString stringWithFormat:@"%@",self.mp4_url];
    
    urlStr =[urlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    
    NSURL *url=[NSURL URLWithString:urlStr];
    
    //创建视频项目对象
    self.playerItem = [[AVPlayerItem alloc] initWithURL:url];
    
    //创建视频播放器对象
    
    self.player = [AVPlayer playerWithPlayerItem:self.playerItem];
    
    [_playerItem release];
    
    //创建承载播放器的AVPlayerLayer对象
    self.playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.player];
    
    self.playerLayer.frame = CGRectMake(0, 0, SCREEN_WIDTH, 300);
    
    //将播放器添加到playerView的layer层上
    [self.viewForVideo.layer insertSublayer:self.playerLayer atIndex:0];
    
    //视频填充模式
    self.playerLayer.videoGravity = AVLayerVideoGravityResizeAspect;
}





#pragma mark 添加观察者
- (void)addObservers
{
    // 监听status属性
    [self.playerItem addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew context:nil];
    
    // 监听loadedTimeRanges属性
    [self.playerItem addObserver:self forKeyPath:@"loadedTimeRanges" options:NSKeyValueObservingOptionNew context:nil];
    
    
    // 添加视频播放结束通知
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(moviePlayDidEnd:) name:AVPlayerItemDidPlayToEndTimeNotification object:_playerItem];
    
    // 监听播放状态
    self.playbackTimeObserver =  [self.player addPeriodicTimeObserverForInterval:CMTimeMake(1, 1) queue:dispatch_get_main_queue () usingBlock:^(CMTime time) {
        
        // 计算当前在第几秒
        CGFloat currentSecond = self.playerItem.currentTime.value/self.playerItem.currentTime.timescale;
        
        [self.sliderForVideoProgress setValue:currentSecond animated:YES];
        
        NSString *timeString = [self convertTime:currentSecond];
        
        self.lableForTime.text = [NSString stringWithFormat:@"%@/%@",timeString,self.strForTotalTime];
    }];
}




#pragma mark 销毁观察者
-(void)removeObservers
{
    [self.playerItem removeObserver:self forKeyPath:@"status" context:nil];
    
    [self.playerItem removeObserver:self forKeyPath:@"loadedTimeRanges" context:nil];
    
    [[NSNotificationCenter defaultCenter] removeObserver:self name:AVPlayerItemDidPlayToEndTimeNotification object:self.playerItem];
    
    [self.player removeTimeObserver:self.playbackTimeObserver];
}



#pragma mark KVO方法
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    AVPlayerItem *playerItem = (AVPlayerItem *)object;
    if ([keyPath isEqualToString:@"status"])
    {
        if ([playerItem status] == AVPlayerStatusReadyToPlay)
        {
            
            self.buttonForState.enabled = YES;
            
            self.isPlay = YES;
            
            [self.player play];
            
            [self.buttonForState setTitle:@"Stop" forState:UIControlStateNormal];
            
            // 获取视频总长度
            CMTime duration = self.playerItem.duration;
            
            // 转换成秒
            CGFloat totalSecond = playerItem.duration.value / playerItem.duration.timescale;
            
            
            // 转换成播放时间
            self.strForTotalTime = [self convertTime:totalSecond];
    
            // 自定义UISlider外观
            [self customVideoSlider:duration];
            
        }
        else if ([playerItem status] == AVPlayerStatusFailed)
        {
            NSLog(@"AVPlayerStatusFailed");
            
            [self.player play];
        }
    } else if ([keyPath isEqualToString:@"loadedTimeRanges"])
    {
        // 计算缓冲进度
        NSTimeInterval timeInterval = [self availableDuration];
        
        CMTime duration = _playerItem.duration;
        
        CGFloat totalDuration = CMTimeGetSeconds(duration);
        
        [self.progressViewForVideoCache setProgress:timeInterval / totalDuration animated:YES];
    }
}



#pragma mark 计算缓存进度
- (NSTimeInterval)availableDuration
{
    NSArray *loadedTimeRanges = [[self.player currentItem] loadedTimeRanges];
    
    // 获取缓冲区域
    CMTimeRange timeRange = [loadedTimeRanges.firstObject CMTimeRangeValue];
    
    float startSeconds = CMTimeGetSeconds(timeRange.start);
    
    float durationSeconds = CMTimeGetSeconds(timeRange.duration);
    
    // 计算缓冲总进度
    NSTimeInterval result = startSeconds + durationSeconds;
    
    return result;
}




#pragma mark 转化时间
- (NSString *)convertTime:(CGFloat)second
{
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:second];
    
    self.dateFormatter = [[NSDateFormatter alloc] init];
    
    [self.dateFormatter setDateFormat:@"mm:ss"];
    
    NSString *showtimeNew = [self.dateFormatter stringFromDate:date];
    
    return showtimeNew;
}




#pragma mark 自定义UISlider外观
- (void)customVideoSlider:(CMTime)duration
{
    self.sliderForVideoProgress.maximumValue = CMTimeGetSeconds(duration);
    
    UIGraphicsBeginImageContextWithOptions((CGSize){ 1, 1 }, NO, 0.0f);
    
    UIImage *transparentImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    [self.sliderForVideoProgress setMinimumTrackImage:transparentImage forState:UIControlStateNormal];
    
    [self.sliderForVideoProgress setMaximumTrackImage:transparentImage forState:UIControlStateNormal];
}





#pragma mark 添加点击手势
- (void)addGestureRecognizer
{
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
    
    [self.viewForVideo addGestureRecognizer:tap];
}





#pragma mark 手势点击所触发的方法
-(void)tapAction
{
    if (self.isTap == NO)
    {
        
        [UIView animateWithDuration:0.5 animations:^{
            
            self.lableForTitle.alpha = 0;
            
            self.buttonForBack.alpha = 0;
            
            self.viewForControlButton.alpha = 0;
            
        }];
        
    }else
    {
        [UIView animateWithDuration:0.5 animations:^{
            
            self.lableForTitle.alpha = 1;
            
            self.buttonForBack.alpha = 1;
            
             self.viewForControlButton.alpha = 1;
        }];
    }
    
    self.isTap = !self.isTap;
}





#pragma mark 创建TabView
- (void)createTabView
{
    self.tabView = [[UITableView alloc] init];
    
    self.tabView.frame = CGRectMake(0, 300, SCREEN_WIDTH, SCREEN_HEIGHT - 300);
    
    [self.view addSubview:self.tabView];
    
    
    UIColor *bgColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"木质.jpg"]];
                        
    self.tabView.backgroundColor = bgColor;
    
    [_tabView release];
    
    //指定代理人
    self.tabView.dataSource = self;
    self.tabView.delegate = self;
    
    //注册Cell
    [self.tabView registerClass:[CellForVideoRecommendation class] forCellReuseIdentifier:@"pool1"];
    
    
    //去掉分割线
    self.tabView.separatorStyle = UITableViewCellSeparatorStyleNone;
}




#pragma mark UITableViewDataSource 协议方法
#pragma mark 相应分区的行的个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arrTitle.count;
}




#pragma mark 给每行添加cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

        CellForVideoRecommendation *cell = [tableView dequeueReusableCellWithIdentifier:@"pool1"];
        
        //去掉选择cell之后背景颜色灰的效果
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        NSString *timeLength = self.arrLength[indexPath.row];
        
        cell.lableForLength.text = [[NSString alloc] initWithFormat:@"视频时长 %02d:%02d", timeLength.intValue / 60, timeLength.intValue % 60];
    
        [cell.imgViewForCover sd_setImageWithURL:[NSURL URLWithString:self.arrCover[indexPath.row]] placeholderImage:[UIImage imageNamed:@"占位图"]];
    
        cell.lableForTitle.text = [self.arrTitle objectAtIndex:indexPath.row];
    
    
    
    cell.lableForTitle.numberOfLines = 0;
    
    cell.lableForTitle.textColor = [UIColor blackColor];
    
    cell.lableForTitle.font = [UIFont systemFontOfSize:15];
    

    
    cell.lableForLength.textColor = [UIColor blackColor];
    
    cell.lableForLength.font = [UIFont systemFontOfSize:15];
    
    cell.tag = indexPath.row;
    
    
    if (cell.tag == self.number)
    {
        [cell.lableForTitle setFont:[UIFont fontWithName:@"Helvetica-Bold" size:18]];
        
        cell.lableForTitle.textColor = [UIColor redColor];
        
        cell.lableForLength.textColor = [UIColor blueColor];
    }
    
    

    return cell;
}




#pragma mark UITableViewDelegate 协议方法
#pragma mark 设置行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}


#pragma mark 添加头标
- (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return @"热门推荐";
}





#pragma mark 选择相应的行所触发的方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.number != indexPath.row)
    {
        [self.player pause];
        
        //销毁观察者
        [self removeObservers];
        
        self.number = indexPath.row;
        
        [self.tabView reloadData];
        
        CellForVideoRecommendation *cell = [tableView cellForRowAtIndexPath:indexPath];
        
        [cell.lableForTitle setFont:[UIFont fontWithName:@"Helvetica-Bold" size:18]];
        
        cell.lableForTitle.textColor = [UIColor redColor];
        
        cell.lableForLength.textColor = [UIColor blueColor];

        
        
        self.lableForTitle.text = self.arrTitle[indexPath.row];
        
        self.mp4_url = self.arrMp4_url[indexPath.row];
        
        self.isPlay = NO;
        
        self.buttonForState.enabled = NO;
        
        self.lableForTime.text = @"Time";
        
        self.sliderForVideoProgress.value = 0;
        
        [self.progressViewForVideoCache setProgress:0. animated:NO];
        
        [self.buttonForState setTitle:@"Play" forState:UIControlStateNormal];
        
        
        self.mp4_url = self.arrMp4_url[indexPath.row];
        
        NSString *urlStr=[NSString stringWithFormat:@"%@",self.mp4_url];
        
        urlStr =[urlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
        
        NSURL *url=[NSURL URLWithString:urlStr];
        
        //创建视频项目对象
        self.playerItem = [AVPlayerItem playerItemWithURL:url];
        
        //添加观察者
        [self addObservers];
        
        [self.player replaceCurrentItemWithPlayerItem:self.playerItem];
    }
}










@end
